<template>
  <van-button
    v-if="isFollowed"
    class="follow-btn"
    type="default"
    round
    size="small"
    @click="doFollow"
    :loading="loading"
    >已关注
  </van-button>
  <van-button
    v-else
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    @click="doFollow"
    :loading="loading"
    >关注
  </van-button>
</template>

<script>
// 导入关注取消api
import { addFollow, delFollow } from '@/api/user'
export default {
  name: 'FollowUser',
  model: {
    prop: 'isFollowed',
    event: 'change'
  },

  props: {
    // 关注状态
    isFollowed: {
      required: true,
      type: Boolean
    },
    // 作者id
    authorId: {
      required: true,
      type: [String, Number]
    }
  },
  data () {
    return {
      loading: false
    }
  },

  mounted () {},

  methods: {
    // 执行关注/取消关注
    async doFollow () {
      this.loading = true
      try {
        if (this.isFollowed) {
          await delFollow(this.authorId)
        } else {
          await addFollow(this.authorId)
        }
        this.$emit('change', !this.isFollowed)
        this.$toast.success('操作成功')
      } catch (error) {
        console.dir(error)
        this.$toast.fail('操作失败')
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
